package com.kupurui.asteacher.ui.job;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.frame.util.AppJsonUtil;
import com.android.frame.view.dialog.NiftyDialogBuilder;
import com.android.frame.view.gridview.GridViewForScrolview;
import com.facebook.drawee.view.SimpleDraweeView;
import com.kupurui.asteacher.R;
import com.kupurui.asteacher.adapter.JobStudentDetailsAdapter;
import com.kupurui.asteacher.bean.JobStudentDetailsInfo;
import com.kupurui.asteacher.http.Teacher;
import com.kupurui.asteacher.ui.BaseAty;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 学生作业
 * Created by Administrator on 2017/7/25.
 */

public class JobStudentDetailsAty extends BaseAty {


    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.imgv_head)
    SimpleDraweeView imgvHead;
    @Bind(R.id.progress_bar_personage)
    ProgressBar progressBarPersonage;
    @Bind(R.id.progress_bar_class)
    ProgressBar progressBarClass;
    @Bind(R.id.gridview)
    GridViewForScrolview gridView;
    @Bind(R.id.tv_num)
    TextView tvNum;
    @Bind(R.id.ll_teacher_comment)
    LinearLayout llTeacherComment;
    @Bind(R.id.tv_content)
    TextView tvContent;
    @Bind(R.id.tv_check_details)
    TextView tvCheckDetails;
    @Bind(R.id.tv_comment)
    TextView tvComment;
    @Bind(R.id.tv_single)
    TextView tvSingle;
    @Bind(R.id.tv_total)
    TextView tvTotal;

    EditText etContent;

    List<JobStudentDetailsInfo.DetailBean> list;
    JobStudentDetailsAdapter adapter;
    JobStudentDetailsInfo jobStudentDetailsInfo;

    NiftyDialogBuilder dialogBuilder;


    private String work_id = "";//作业ID
    private String st_id = "";//学生ID
    private String class_id = "";//班级ID
    private String name = "";//学生姓名

    private boolean is_correct = false;

    @Override
    public int getLayoutId() {
        return R.layout.job_student_details_aty;
    }

    @Override
    public void initData() {
        if (getIntent().getExtras() != null) {
            work_id = getIntent().getStringExtra("id");
            st_id = getIntent().getStringExtra("st_id");
            name = getIntent().getStringExtra("name");
            class_id = getIntent().getStringExtra("class_id");
        }
        list = new ArrayList<>();
        adapter = new JobStudentDetailsAdapter(this, list, R.layout.job_student_datails_item);
        gridView.setAdapter(adapter);
        tvTitle.setText(name);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (list.get(position).getStatus().equals("1")){
                    List<String> daiList = new ArrayList<>();
                    int fgtIndex = 0;
                    for (int i=0;i<list.size();i++){
                        if (list.get(i).getStatus().equals("1")){
                            daiList.add(list.get(i).getQ_id());
                        }
                    }

                    for (int i=0;i<daiList.size();i++){
                        if (daiList.get(i).contains(list.get(position).getQ_id())){
                            fgtIndex = i;
                        }
                    }
                    Bundle bundle = new Bundle();
                    bundle.putString("id",work_id);
                    bundle.putString("class_id",class_id);
                    bundle.putInt("fgtindex",fgtIndex);
                    startActivity(TopicDetailsAty.class,bundle);
                }
            }
        });
    }

    @Override
    public void requestData() {
        showLoadingContent();
        new Teacher().userAnswer(work_id, st_id,class_id ,this, 0);
    }

    @Override
    protected void onResume() {
        super.onResume();
        new Teacher().userAnswer(work_id, st_id, class_id,this, 0);
    }

    @OnClick({R.id.iv_back, R.id.tv_comment,R.id.tv_check_details})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_comment://填写评语
                is_correct = false;
                for (int i = 0;i<list.size();i++){
                    if (list.get(i).getStatus().equals("1")){
                        is_correct = true;
                    }
                }
                if (is_correct){
                    showToast("请批改完再填写评语");
                    return;
                }
                if (dialogBuilder == null) {
                    showDialog();
                } else {
                    dialogBuilder.show();
                }
                break;
            case R.id.tv_check_details://查看作业详情
                Bundle bundle = new Bundle();
                bundle.putString("id",work_id);
                bundle.putString("st_id",st_id);
                startActivity(JobAnalysisAty.class,bundle);
                break;
        }
    }

    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        switch (what) {
            case 0:
                jobStudentDetailsInfo = AppJsonUtil.getObject(result, JobStudentDetailsInfo.class);
                progressBarPersonage.setProgress(jobStudentDetailsInfo.getSingle());
                progressBarClass.setProgress(jobStudentDetailsInfo.getTotal());
                tvNum.setText(jobStudentDetailsInfo.getRank());
                imgvHead.setImageURI(jobStudentDetailsInfo.getImg());
                tvSingle.setText(jobStudentDetailsInfo.getSingle()+"%");
                tvTotal.setText(jobStudentDetailsInfo.getTotal()+"%");
                if (TextUtils.isEmpty(jobStudentDetailsInfo.getComment())) {
                    tvComment.setVisibility(View.VISIBLE);
                    llTeacherComment.setVisibility(View.GONE);
                }else{
                    tvComment.setVisibility(View.GONE);
                    llTeacherComment.setVisibility(View.VISIBLE);
                    tvContent.setText(jobStudentDetailsInfo.getComment());
                }
                list.clear();
                list.addAll(jobStudentDetailsInfo.getDetail());
                adapter.notifyDataSetChanged();
                break;
            case 1:
                new Teacher().userAnswer(work_id, st_id, class_id,this, 0);
                showToast(AppJsonUtil.getResultInfo(result).getMessage());
                dialogBuilder.dismiss();
                break;
        }
        super.onSuccess(result, call, response, what);
    }

    private void showDialog() {
        dialogBuilder = new NiftyDialogBuilder(this);
        View view = LayoutInflater.from(this).inflate(R.layout.comment_dialog, null);
        dialogBuilder.setND_AddCustomView(view);
        dialogBuilder.setNd_IsOnTouchOutside(true);
        etContent = (EditText) view.findViewById(R.id.et_content);
        TextView tvSubmit = (TextView) view.findViewById(R.id.tv_submit);
        tvSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                comment();
            }
        });
        dialogBuilder.show();
    }

    private void comment(){
        String content = etContent.getText().toString();
        if (TextUtils.isEmpty(content)){
            showToast("请输入您对这次作业的评价...");
            return;
        }
        showLoadingDialog("");
        new Teacher().comment(work_id,st_id,content,this,1);
    }

}
